;
(function() {
	var skill = {
		bindTicketToAch: function(ticket_id, ach_id) {
			var ach = this._getAch(ach_id);
			return ach._bindTicket(ticket_id);
		},
		_getTicketIdList: function() {
			var ticket_list = this.ticket_list;
			if (!Array.isArray(ticket_list)) {
				ticket_list = this.ticket_list = [];
				this.save();
			}
			return ticket_list;
		},
		getTicketList: function(state) {
			state = ~~state;
			var ticket_list = this._getTicketIdList().map(function(ticket_id) {
				return TicketCon.get(ticket_id);
			});
			if (state) {
				ticket_list = ticket_list.filter(function(ticket) {
					return ticket.state === state;
				});
			}
			return ticket_list;
		},
		_getTicket: function(ticket_id) {
			var ticket_list = this._getTicketIdList();
			if (ticket_list.indexOf(ticket_id) === -1) {
				throwCusError("该优惠券不属于指定用户")
			}
			return TicketCon.get(ticket_id);
		},
		getUseAbleTickListForAch: function(ach_id) {
			var ach = this._getAch(ach_id);
			var ticket_list = this.getTicketList(TicketCon.STATE.未使用).filter(function(ticket) {
				return ticket._canUseForAch(ach);
			});
			return ticket_list;
		},
		//只能领取增加，不可删除不可修改
		addTicketFromTicketFactory: function(ticket_factory_id) {
			var ticket_factory = TicketFactoryCon.get(ticket_factory_id);
			if (!ticket_factory) {
				throwCusError("找不到指定优惠券可领取")
			}
			if (ticket_factory.info.forbid_user_get) {
				throwCusError("用户无法直接领取此优惠券")
			}
			var ticket = ticket_factory.toTicket(this._id);
			this._getTicketIdList().unshift(ticket._id);
			this.save();
			return ticket;
		}
	};
	module.exports = skill;
	var db = require("../db/index");
	var CouponCon = require("../coupon/_con");
	var TicketCon = require("../ticket/_con");
	var TicketFactoryCon = require("../ticket_factory/_con");
	var DomainCon = require("../domain/_con");
	var GoodsCon = require("../goods/_con");
	var UserCon = require("../../core/user/_con");
	var BusCon = require("../../core/bus/_con");
	var AchCon = require("../ach/_con");
	var ClassifyCon = require("../bus.classify/_con");
	var TagCon = require("../bus.tag/_con");
	var LeagueCon = require("../league/_con");
	var BuffCon = require("../buff/_con");
	var BusUserBindCon = require("../bus_user_bind/_con");
	var BusGuideCon = require("../bus.guide/_con");
	var CommentCon = require("../comment/_con");
	var config = require("../_include/config");
	var data_cut = require("../_include/data_cut");
	var helper = require("../_include/helper");
	var md5 = require("../_include/md5");
	var $Object = require("../_include/$.Object");
	var $Array = require("../_include/$.Array");
	var tld = require('tldjs');
	var fs = require("fs");
	var Fiber = require('fibers');
}());